package com.haidechizi.fastadmin.common.utils;

import com.baomidou.mybatisplus.plugins.Page;
import com.haidechizi.fastadmin.common.xss.SQLFilter;

import java.util.Map;

public class PageUtils {

    public static <T> Page<T> cover2Page(Map<String, Object> params) {
        int currPage = 1;
        int limit = 10;
        //分页参数
        if (params.get("page") != null) {
            currPage = Integer.parseInt((String) params.get("page"));
        }
        if (params.get("limit") != null) {
            limit = Integer.parseInt((String) params.get("limit"));
        }
        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = SQLFilter.sqlInject((String) params.get("sidx"));
        String order = SQLFilter.sqlInject((String) params.get("order"));
        Page<T> page = new Page<>(currPage, limit);
        //排序
        if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) {
            page.setOrderByField(sidx);
            page.setAsc("ASC".equalsIgnoreCase(order));
        }
        return page;
    }
}
